{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 第二章.感知机的实现\n",
    "## 2.1.感知机是什么？\n",
    "<p>感知机接受多个输入信号,输出一个信号.感知机的信号只有\"流/不流(1/0)两种取值\"。所有输入信号都有子集的权重,当感知机的权重和大于阈值时,感知机将输出1（输出信号）</p>\n",
    "\n",
    "### 2.2.简单逻辑电路\n",
    "<p>常见的门有:与门,与非门,或门,异或门。当到达对应条件时,这些门会输出信息.我们可以使用感知机表示门</p>\n",
    "\n",
    "### 2.3.感知机的实现\n",
    "#### 2.3.1.简单实现\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "0\n",
      "0\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "def AND(x1,x2): # 实现与门\n",
    "    w1,w2,theta = 0.5,0.5,0.7\n",
    "    tmp = x1*w1 + x2*w2\n",
    "    if tmp<=theta:\n",
    "        return 0\n",
    "    elif tmp >theta:\n",
    "        return 1\n",
    "# 当输入的加权总和超过阈值时,返回1，否侧返回0\n",
    "print(AND(0,0))\n",
    "print(AND(1,0))\n",
    "print(AND(0,1))\n",
    "print(AND(1,1))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-09-25T04:34:41.306540100Z",
     "start_time": "2023-09-25T04:34:41.300521400Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.3.2.导入权重和偏置\n",
    "<p>我们可以将阈值移到方程左端,使右端为0.这样我们把阈值称为偏置b</p>\n",
    "\n",
    "####  2.3.3.使用权重和偏置"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "与门\n",
      "0\n",
      "0\n",
      "0\n",
      "1\n",
      "-------------------\n",
      "与非门\n",
      "1\n",
      "1\n",
      "1\n",
      "0\n",
      "--------------------\n",
      "或门\n",
      "0\n",
      "1\n",
      "1\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "def AND(x1,x2):\n",
    "    x = np.array([x1,x2])\n",
    "    # w为权重\n",
    "    w = np.array([0.5,0.5])\n",
    "    # b为偏置\n",
    "    b= -0.7\n",
    "    tmp = np.sum(w*x)+b\n",
    "    if tmp >0:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0\n",
    "\n",
    "print('与门')\n",
    "print(AND(0,0))\n",
    "print(AND(1,0))\n",
    "print(AND(0,1))\n",
    "print(AND(1,1))\n",
    "print(\"-------------------\")\n",
    "\n",
    "# 与非门\n",
    "def NAND(x1,x2):\n",
    "    x = np.array([x1,x2])\n",
    "    w = np.array([-0.5,-0.5])\n",
    "    b = 0.7\n",
    "    tmp = np.sum(w*x)+b\n",
    "    if tmp<=0:\n",
    "        return 0\n",
    "    else:\n",
    "        return 1\n",
    "print('与非门')\n",
    "print(NAND(0,0))\n",
    "print(NAND(0,1))\n",
    "print(NAND(1,0))\n",
    "print(NAND(1,1))\n",
    "print(\"--------------------\")\n",
    "\n",
    "def OR(x1,x2):\n",
    "    x = np.array([x1,x2])\n",
    "    w = np.array([0.5,0.5])\n",
    "    b = -0.2\n",
    "    tmp = np.sum(x*w)+b\n",
    "    if tmp <=0:\n",
    "        return 0\n",
    "    else:\n",
    "        return 1\n",
    "\n",
    "print('或门')\n",
    "print(OR(0,0))\n",
    "print(OR(0,1))\n",
    "print(OR(1,0))\n",
    "print(OR(1,1))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-09-25T04:53:12.926520500Z",
     "start_time": "2023-09-25T04:53:12.882497600Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.4.感知机的局限性\n",
    "<p>单个感知机的局限型在于只能表示线性空间,而不能表示非线性空间。为了表示非线性空间，我们可以将多个感知机组合在一起，形成多层感知机</p>\n",
    "\n",
    "## 2.5.多层感知机\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "异或门\n",
      "0\n",
      "1\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "# 异或门的实现\n",
    "def XOR(x1,x2):\n",
    "    s1 = NAND(x1,x2)\n",
    "    s2 = OR(x1,x2)\n",
    "    y = AND(s1,s2)\n",
    "    return y\n",
    "\n",
    "print(\"异或门\")\n",
    "print(XOR(0,0))\n",
    "print(XOR(0,1))\n",
    "print(XOR(1,0))\n",
    "print(XOR(1,1))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-09-25T05:13:24.824719300Z",
     "start_time": "2023-09-25T05:13:24.815711800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
